import { BattleSkill } from '.';
import {
  BATTLE_SKILL_TARGET,
  BATTLE_SKILL_TYPE,
} from '../../data/battle-skill';
import { BATTLE_SIDE, type BattleUnit } from '../battle/unit';

export class BattleSkillCalculator {
  /**
   * 计算技能效果值
   * @param skill 技能
   * @param source 施法者
   * @param target 目标
   * @returns 技能最终伤害
   */
  static calculateSkillValue(
    skill: BattleSkill,
    source: BattleUnit,
    target: BattleUnit
  ): number {
    const sourceAttribute = source.getBattleSkillAttributes();
    const targetAttribute = target.getBattleSkillAttributes();
    /** 计算基础伤害 */
    let baseValue = skill.baseValue || 0;
    let multiplier = skill.multiplier || 1;
    let baseAttribute = skill.baseAttribute;
    if (baseAttribute && sourceAttribute[baseAttribute]) {
      const attributeValue = sourceAttribute[baseAttribute];
      baseValue += attributeValue * multiplier;
    }

    /** 减去目标防御 */
    baseValue = Math.max(1, baseValue - targetAttribute.DEFENSE);

    /** 物理攻击 10% 暴击，暴击伤害 1.5 倍（TODO: 临时） */
    if (skill.type === BATTLE_SKILL_TYPE.PHYSICAL) {
      if (Math.random() < 0.1) {
        baseValue *= 1.5;
      }
    }

    return baseValue;
  }

  /**
   * 获取技能目标列表
   * @param skill 技能配置
   * @param source 施法者单位
   * @param teammates 队友列表
   * @param enemies 敌人列表
   */
  static getSkillTargets(
    skill: BattleSkill,
    source: BattleUnit,
    teammates: BattleUnit[],
    enemies: BattleUnit[]
  ): BattleUnit[] {
    const gainType: string[] = [
      BATTLE_SKILL_TYPE.BUFF,
      BATTLE_SKILL_TYPE.CLEANSE,
      BATTLE_SKILL_TYPE.HEAL,
      BATTLE_SKILL_TYPE.SHIELD,
    ];
    let reallyTeammates: BattleUnit[] = [];
    let reallyEnemies: BattleUnit[] = [];
    if (source.side === BATTLE_SIDE.PLAYER) {
      reallyTeammates = teammates;
      reallyEnemies = enemies;
    } else {
      reallyTeammates = enemies;
      reallyEnemies = teammates;
    }
    /** 给予友方 */
    if (gainType.includes(skill.type)) {
      /** 存活的队友 */
      const aliveTeammates = reallyTeammates.filter((unit) => unit.isAlive());
      switch (skill.target) {
        case BATTLE_SKILL_TARGET.SELF:
          return [source];
        case BATTLE_SKILL_TARGET.TEAMMATE:
          if (aliveTeammates.length === 0) return [];
          /** 血量最低的队友 */
          return [
            aliveTeammates.reduce((lowest, current) =>
              current.currentHealth < lowest.currentHealth ? current : lowest
            ),
          ];
        case BATTLE_SKILL_TARGET.ALL_ALLIES:
          return [...aliveTeammates];
        default:
          return [];
      }
    } else {
      /** 存活的敌人 */
      const aliveEnemies = reallyEnemies.filter((unit) => unit.isAlive());
      switch (skill.target) {
        case BATTLE_SKILL_TARGET.ENEMY:
          /** 随机一个敌方（简单 AI：血量最低的） */
          if (aliveEnemies.length === 0) return [];
          /** 血量最低的敌人 */
          return [
            aliveEnemies.reduce((lowest, current) =>
              current.currentHealth < lowest.currentHealth ? current : lowest
            ),
          ];
        case BATTLE_SKILL_TARGET.ALL_ENEMIES:
          return [...aliveEnemies];
        default:
          return [];
      }
    }
  }

  /**
   * 检查技能是否可用
   */
  static isSkillAvailable(
    skill: BattleSkill,
    currentMana: number,
    currentCooldown: number
  ): boolean {
    if (skill.cost && currentMana < skill.cost) {
      return false;
    }

    if (skill.cooldown && currentCooldown > 0) {
      return false;
    }

    return true;
  }
}
